//
//  LeftTebleViewController.m
//  Flava
//
//  Created by Ug-Code on 13-8-14.
//  Copyright (c) 2013年 UgCode. All rights reserved.
//

#import "LeftTableViewController.h"
#import "LeftTableCellModel.h"

#define kLeftTebleView_X 0
#define kLeftTebleView_Y 180
#define kLeftTebleView_Y_7 175
#define kLeftTebleView_W 320
#define kLeftTebleView_H [UGDevice windowHeight]-20-kLeftTebleView_Y-49-5
#define kLeftTebleView_H_7 [UGDevice windowHeight]-kLeftTebleView_Y_7-49

@interface LeftTableViewController ()

@end

@implementation LeftTableViewController

@synthesize
tableView = _tableView,
tableCellModels = _tableCellModels;

- (id)init
{
    self = [super init];
    if (self) {
		if ([UGDevice systemVersion] >= 7) {
			_tableView = [[UITableView alloc]
						  initWithFrame:CGRectMake(kLeftTebleView_X, kLeftTebleView_Y_7, kLeftTebleView_W, kLeftTebleView_H_7)];
		} else {
			_tableView = [[UITableView alloc]
						  initWithFrame:CGRectMake(kLeftTebleView_X, kLeftTebleView_Y, kLeftTebleView_W, kLeftTebleView_H)];
		}
		_tableView.delegate = self;
		_tableView.dataSource = self;
		_tableView.allowsSelection =  NO;
		//不显示cell之间的分割线
		_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
		_tableView.backgroundView = [[UIImageView alloc] initWithImage:
									 [[UIImage imageNamed:@"cell_bg_common2_n.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:5]];
		[UGDevice windowHeight];
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [_tableCellModels count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if ([[_tableCellModels[indexPath.row] cellKey] isEqualToString:kCommonCell]) {
		return 50;
	} else if ([[_tableCellModels[indexPath.row] cellKey] isEqualToString:KAttachedCell]) {
		return 217;
	} else if ([[_tableCellModels[indexPath.row] cellKey] isEqualToString:kEmoticonCell]) {
		return 350;
	} else if ([[_tableCellModels[indexPath.row] cellKey] isEqualToString:kLastCell]) {
		return 50;
	}else {
		return 0;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	//-- 判断cell的类型
	if ([[_tableCellModels[indexPath.row] cellKey] isEqualToString:kCommonCell]) {
		LeftCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:kCommonCell];
		if (cell == nil) {
			cell = [[LeftCommonCell alloc] initWithStyle:(indexPath.row)%2 reuseIdentifier:kCommonCell];
		}
		[cell setTableCellModel:_tableCellModels[indexPath.row]];
		return cell;
	} else if ([[_tableCellModels[indexPath.row] cellKey] isEqualToString:KAttachedCell]) {
		LeftAttachedCell *cell = [tableView dequeueReusableCellWithIdentifier:KAttachedCell];
		if (cell == nil) {
			cell = [[LeftAttachedCell alloc] initWithStyle:(indexPath.row)%2 reuseIdentifier:KAttachedCell];
		}
		[cell setTableCellModel:_tableCellModels[indexPath.row]];
		return cell;
	} else if ([[_tableCellModels[indexPath.row] cellKey] isEqualToString:kEmoticonCell]) {
		LeftEmoticonCell *cell = [tableView dequeueReusableCellWithIdentifier:kEmoticonCell];
		if (cell == nil) {
			cell = [[LeftEmoticonCell alloc] initWithStyle:(indexPath.row)%2 reuseIdentifier:kEmoticonCell];
		}
		[cell setTableCellModel:_tableCellModels[indexPath.row]];
		return cell;
	} else if ([[_tableCellModels[indexPath.row] cellKey] isEqualToString:kLastCell]) {
		//-- 最下面版权信息
		UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kLastCell];
		if (cell == nil) {
			cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:kLastCell];
		}
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(98, 10, 68, 10)];
		label.text = [_tableCellModels[indexPath.row] titleText];
		label.textColor = [UIColor grayColor];
		label.font = [UIFont systemFontOfSize:10];
		label.backgroundColor = UGAlphaColor;
		cell.backgroundColor = UGAlphaColor;
		[cell.contentView addSubview:label];
		return cell;
	} else {
		return nil;
	}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
}


@end
